package com.netease.xone.itemview;

import java.util.HashMap;

import android.content.Context;
import android.database.Cursor;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AlphabetIndexer;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.netease.xone.R;
import com.netease.xone.widget.LoadingImageView;

import db.manager.ManagerDiscussTip;


public class ItemViewTipBase extends FrameLayout {
    
    public static final int TYPE_BASE      = ManagerDiscussTip.TYPE_USER;
    public static final int TYPE_USER      = TYPE_BASE;
    public static final int TYPE_SOURCE    = TYPE_BASE + 1;
    public static final int TYPE_TOPIC     = TYPE_BASE + 2;
    public static final int TYPE_SUBJECT   = TYPE_BASE + 3;
    
    private int mType = -1;
    
    protected TextView  mLetter = null;
    protected CheckBox  mCheckBox = null;
    protected View      mLeft = null;
    protected LoadingImageView mIcon = null;
    protected TextView mTitle = null;
    protected TextView mContent = null;
    protected HashMap<Integer, String> mHashMap;

    public ItemViewTipBase(Context context, int type) {
        super(context);
        this.mType = type;
        init();
    }

    public ItemViewTipBase(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ItemViewTipBase(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        View v = null;
        int width = 0;
        switch (mType) {
            case ItemViewTipBase.TYPE_USER:
            case ItemViewTipBase.TYPE_SOURCE:
            case ItemViewTipBase.TYPE_SUBJECT:
                width = getContext().getResources().getDimensionPixelSize(R.dimen.discuss_size_50);
                v = View.inflate(getContext(), R.layout.item_view_tip_base1, null);
                mLetter = (TextView)v.findViewById(R.id.firstLetter);
                mCheckBox = (CheckBox)v.findViewById(R.id.check);
                // 如果不设置成false 整个item点击无效
                mCheckBox.setFocusable(false);
                mIcon = (LoadingImageView)v.findViewById(R.id.icon);
                mIcon.setServerClipSize(width, width);
                mTitle = (TextView)v.findViewById(R.id.title);
                mContent = (TextView)v.findViewById(R.id.content);
                break;
            case ItemViewTipBase.TYPE_TOPIC:
                width = getContext().getResources().getDimensionPixelSize(R.dimen.discuss_size_40);
                v = View.inflate(getContext(), R.layout.item_view_tip_base2, null);
                mLeft = v.findViewById(R.id.relavive_left);
                mIcon = (LoadingImageView)v.findViewById(R.id.icon);
                mIcon.setServerClipSize(width, width);
                mTitle = (TextView)v.findViewById(R.id.title);
                break;
            default:
                break;
        }
        
        addView(v);
    }
    
    public void bindView(Cursor cursor) {
        
    }
    
    public void bindView(Cursor cursor, AlphabetIndexer alpha) {
        if (null == alpha) {
            return;
        }

        int position = cursor.getPosition();
        int section = alpha.getSectionForPosition(position);
        if (alpha.getPositionForSection(section) == position) {
            mLetter.setVisibility(View.VISIBLE);
            String title = alpha.getSections()[section].toString().trim();
            if (position == section && 0 == position) {
                switch (mType) {
                    case TYPE_USER:
                        mLetter.setText(R.string.tip_user_letter);
                        break;
                    case TYPE_SOURCE:
                    	mLetter.setText(R.string.tip_source_letter);
                        break;
                    case TYPE_SUBJECT:
                        mLetter.setText(R.string.tip_subject_letter);
                        break;
                }
            } else {
                mLetter.setText(title);
            }
        } else {
            mLetter.setVisibility(View.GONE);
        }
    }
    
    public void setCheckArray(HashMap<Integer, String> array) {
        this.mHashMap = array;
    }
}
